package day29.edu;

import org.junit.Test;

import java.io.File;
import java.io.IOException;

// Created: 29/09/2025, ChangJiang Ru
public class FileTest {
    @Test
    public void test1()
    {
        File file1 = new File("abc");
        System.out.println(file1.getAbsolutePath());
    }

    public static void main(String[] args) {
        File file1 = new File("bbd");
        System.out.println(file1.getAbsolutePath()); // 
    }
    
    @Test
    public void test2()
    {
        File file = new File("hello.txt");

        System.out.println(file.getName());
        System.out.println(file.getPath());
        System.out.println(file.getAbsolutePath());
        System.out.println(file.getAbsoluteFile());
        System.out.println(file.getParent());
        System.out.println(file.length());
        System.out.println(file.lastModified());
    }
    
    @Test
    public void test3()
    {
        File file = new File("D:\\IDEA");

        String[] fileArr = file.list();
        for(String s : fileArr)
        {
            System.out.println(s);
        }

        System.out.println("=======");

        File[] files = file.listFiles();
        for(File f : files)
        {
            System.out.println(f);
        }
    }
    
    @Test
    public void test4()
    {
        File file1 = new File("hello.txt");

        File file2 = new File("D:\\IO\\ab.txt"); // Must not exist

        boolean renameSuccess = file1.renameTo(file2);

        System.out.println(renameSuccess? "Rename success":"Rename not success");
    }
    
    @Test
    public void test5()
    {
        File file1 = new File("D:\\IO\\ab.txt");

        System.out.println(file1.exists());
        System.out.println(file1.isDirectory());
        System.out.println(file1.isFile());
        System.out.println(file1.canRead());
        System.out.println(file1.canWrite());
        System.out.println(file1.isHidden());
    }
    
    @Test
    public void test6() throws IOException {
        File file1 = new File("D:\\IO\\hello.txt");

        if(!file1.exists())
        {
            if(file1.createNewFile())
            {
                System.out.println("Create successfully");
            }
        }else
        {
            System.out.println("File is not exist");
        }
    }

    @Test
    public void test7()
    {
        File file1 = new File("hello.txt");
        System.out.println(file1.getAbsolutePath());

        System.out.println(file1.getAbsoluteFile().getParent());

        File file2 = new File(file1.getAbsoluteFile().getParent(), "abc.txt");

        System.out.println(file2.getAbsolutePath());
    }
    
    @Test
    public void test8()
    {
        File dir = new File("C:\\Users\\ASUS\\Pictures\\Screenshots");
        String[] str = dir.list();
        for (String s : str)
        {
            if(s.endsWith(".png"))
            {
                System.out.println(s);
            }
        }
    }
}
